package com.yekaile.carsystem.service;

import cn.hutool.core.util.StrUtil;
import com.yekaile.carsystem.controller.vo.OrderFormVO;
import com.yekaile.carsystem.entity.*;
import com.yekaile.carsystem.mapper.OrderFormDetailMapper;
import com.yekaile.carsystem.mapper.OrderFormMapper;
import com.yekaile.carsystem.mapper.UserAddressMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yekaile
 */
@Service
@AllArgsConstructor(onConstructor_=@Autowired)
public class OrderFormServiceImpl implements OrderFormService {


    OrderFormMapper orderFormMapper;
    UserAddressMapper userAddressMapper;
    OrderFormDetailMapper orderFormDetailMapper;



    @Override
    public List<OrderForm> listOrderForm(OrderForm orderForm) {
        OrderFormExample example = new OrderFormExample();
        if (orderForm.getUserId() != null) {
            example.or().andUserIdEqualTo(orderForm.getUserId());
        }
        if (!StrUtil.hasBlank(orderForm.getOrderFormNumber())) {
            example.or().andOrderFormNumberEqualTo(orderForm.getOrderFormNumber());
        }
        if (orderForm.getOrderFormStatus() != null) {
            example.or().andOrderFormStatusEqualTo(orderForm.getOrderFormStatus());
        }
        if (orderForm.getOrderFormPayType() != null) {
            example.or().andOrderFormPayTypeEqualTo(orderForm.getOrderFormPayType());
        }
        return orderFormMapper.selectByExample(example);
    }

    @Override
    public OrderFormVO getOrderFormVO(Integer orderFormId) {
        OrderForm orderForm = orderFormMapper.selectByPrimaryKey(orderFormId);
        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(orderForm.getUserAddressId());
        OrderFormDetailExample example = new OrderFormDetailExample();
        OrderFormDetailExample.Criteria criteria = example.createCriteria();
        criteria.andOrderFormIdEqualTo(orderFormId);
        List<OrderFormDetail> orderFormDetails = orderFormDetailMapper.selectByExample(example);
        return new OrderFormVO(orderForm, orderFormDetails, userAddress);
    }

    @Override
    public Integer deleteOrderForm(Integer orderFormId) {
        OrderForm orderForm = orderFormMapper.selectByPrimaryKey(orderFormId);
        orderForm.setIsDeleted(1);
        return orderFormMapper.updateByPrimaryKeySelective(orderForm);
    }


}
